﻿define('ui/controller/advancedFilterController',
	['local/cache', 'api/polluteService',
	 'api/processTechService', 'api/timeService',
	 'ui/controller/conditionController'],
	function(cache, polluteService, processTechService, timeService, conditionController) {

	var advancedFilterController = {
			
		initial: function(){
			
			//todo : clean selected data
			
			if(cache.getPolluteData() == null) {
				cache.setPolluteData(polluteService.getPollutes());
				advancedFilterController.renderPolluteList(cache.getPolluteData());
			}
			if(cache.getProcessTechData() == null) {
				cache.setProcessTechData(processTechService.getProcessTech());
				advancedFilterController.renderProcessTech(cache.getProcessTechData());
			}
			
			if(cache.getYearData() == null) {
				cache.setYearData(timeService.getYears(5));
				advancedFilterController.renderYearList(cache.getYearData());
			}
			
			
			$("#myButton").click(function(){
				if(!advancedFilterController.validateNumberic($('#permitEmissionFrom')) 
						|| !advancedFilterController.validateNumberic($('#permitEmissionTo'))
						|| !advancedFilterController.validateNumberic($('#actualEmissionFrom')) 
						|| !advancedFilterController.validateNumberic($('#actualEmissionTo'))){
					alert('请填写数字');
					return;
				}
				conditionController.hideConditionList();
				
				//todo：enterprise
				var enterpriseML = require('ui/markerList/enterpriseML');
				enterpriseML.reRenderData();
			});
		},
		
		validateNumberic: function($field) {
	        var value = $field.val();
	        if (value == '') {
	            return true;
	        }
	        return !isNaN(parseFloat(value)) && isFinite(value);
	    },
		
		renderPolluteList: function(data) {
			for(var i=0; i<data.length; i++) {
				$("#permitPolluteCode").append("<option value='"+data[i].polluteCode+"'>"+data[i].polluteName+"</option >");
				$("#actualPolluteCode").append("<option value='"+data[i].polluteCode+"'>"+data[i].polluteName+"</option >");
			}
		},
		
		renderYearList: function(d) {
			for(var i=0; i<d.length; i++) {
				$("#startYear").append("<option value='"+d[i]+"'>"+d[i]+"年</option >");
				$("#endYear").append("<option value='"+d[i]+"'>"+d[i]+"年</option >");
			}
		},
		
		renderProcessTech: function(data) {
			for(var i=0; i<data.length; i++) {
				$("#processTechCheck").append(
					'<label style="margin-right:10px;">'+
						'<input type="checkbox" name="processTechCheckbox" value="'+data[i].techId+'">'+data[i].techName+
					'</label>'
				);
			}
		}
	};
	
	return advancedFilterController;
});